#!/bin/bash
set -vx

# Set android abi

ABI="armeabi-v7a"
# ABI="x86"
# ABI="arm64-v8a"
# ABI="linux"

# set source
# TENSORFLOW_SOURCE_DIR="E:/source_other/tensorflow"
TENSORFLOW_SOURCE_DIR=`pwd`/..
SOURCE_DIR=`pwd`

# set tools
# NDK 21.1.6352462 have BUG, can't compile "arm64-v8a" ABI
NDK_DIR="${ANDROID_HOME}/ndk/23.1.7779620/"
CMAKE_DIR="${ANDROID_HOME}/cmake/3.22.1/bin/"
# CMAKE_DIR="${ANDROID_HOME}/cmake/3.18.1/bin/"

echo build `ls -d ${SOURCE_DIR}`...

arg_clean=""
arg_config=""
arg_build=""
arg_install=""
arg_target=""
arg_set=""

for arg in "$@"
do
	case "$arg" in 
	clean)
		arg_clean=true
		;;
	config)
		arg_config=true
		;;
	build)
		arg_build=true
		;;
	install)
		arg_install=true
		;;
	target)
		arg_set="target"
		;;
	x86)
		ABI="x86"
	;;
	x64)
		ABI="x64"
	;;
	arm)
		ABI="armeabi-v7a"
	;;
	arm64)
		ABI="arm-v8a"
	;;
	linux)
		ABI="linux"
	;;
	*)
		case "$arg_set" in 
			target)
				arg_set=""
				arg_target="$arg"
				arg_target_flag="--target $arg"
			;;
		esac
	;;
	esac
done

if [ ${BUILD_DIR} != "/*" ] && [ ${BUILD_DIR} != "\\*" ]
then
	BUILD_DIR=`pwd`/${BUILD_DIR}
fi

# set output
BUILD_DIR=".cxx/${ABI}/"
OUTPUT_DIR="./build_output/${ABI}/"

if [ "$ABI" != "linux" ]
then
	ANDROID_ABI=true
else
	CMAKE_DIR=""
fi

if [ -n "$arg_target" ]
then
	SOURCE_DIR=`pwd`/"$arg_target"
fi

# clean cmake cache
if [ -n "$arg_clean" ]
then
	rm ${BUILD_DIR}CMakeCache.txt -f
	sleep 1
fi

# configure cmake
if [ -n "$arg_config" ]
then
	CMAKE_ENV="
		-DABI=${ABI} 
		-DCMAKE_VERBOSE_MAKEFILE=ON
		-DCMAKE_AUTOGEN_VERBOSE=ON
		-DCMAKE_MESSAGE_LOG_LEVEL=TRACE
		-DOVERRIDABLE_FETCH_CONTENT_USE_GIT=ON
		-DFETCHCONTENT_QUIET=OFF
		-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
		-DCMAKE_CXX_FLAGS=-std=c++17
		-DCMAKE_CXX_STANDARD=17
		-DCMAKE_CXX_STANDARD_REQUIRED=ON
		-DFETCHCONTENT_UPDATES_DISCONNECTED=ON"
		# -DFETCHCONTENT_BASE_DIR=${BUILD_DIR}../${ABI}_deps
	#	-DFETCHCONTENT_FULLY_DISCONNECTED=ON
	
		if [ -n "$ANDROID_ABI" ]
		then CMAKE_ENV="${CMAKE_ENV}
			-DCMAKE_SYSTEM_NAME=Android
			-DCMAKE_SYSTEM_VERSION=19
			-DANDROID_PLATFORM=android-19
			-DANDROID_ABI=${ABI}
			-DCMAKE_ANDROID_ARCH_ABI=${ABI} 
			-DANDROID_NDK=${NDK_DIR} 
			-DCMAKE_ANDROID_NDK=${NDK_DIR} 
			-DCMAKE_TOOLCHAIN_FILE=${NDK_DIR}build/cmake/android.toolchain.cmake 
			-DCMAKE_MAKE_PROGRAM=${CMAKE_DIR}ninja.exe
			-GNinja
			"
		fi

	TENSORFLOW_OPT="
		-DABSL_PROPAGATE_CXX_STD=ON
		-DTFLITE_ENABLE_RUY=ON
		-DTFLITE_ENABLE_GPU=ON
		-DTFLITE_ENABLE_NNAPI=OFF
		-DTFLITE_ENABLE_XNNPACK=OFF
		-DTFLITE_ENABLE_MMAP=OFF
		-DFLATBUFFERS_BUILD_FLATC=OFF "
	TENSORFLOW_INS="
		-DABSL_ENABLE_INSTALL=ON
		-DFLATBUFFERS_INSTALL=ON
		-DRUY_ENABLE_INSTALL=ON
		-DTFLITE_ENABLE_INSTALL=ON "
	mkdir -p "${BUILD_DIR}"
	pushd "${BUILD_DIR}"
	${CMAKE_DIR}cmake \
		-DCMAKE_BUILD_TYPE=Debug \
		${CMAKE_ENV} \
		${TENSORFLOW_OPT} \
		${TENSORFLOW_INS} \
		-DTENSORFLOW_SOURCE_DIR=${TENSORFLOW_SOURCE_DIR} \
		-B${BUILD_DIR} \
		-S${SOURCE_DIR}		
	popd
fi

# build source
if [ -n "$arg_build" ]
then
	[ $? == 0 ] &&
	${CMAKE_DIR}cmake --build ${BUILD_DIR} -j $arg_target_flag
fi

# install target
if [ -n "$arg_install" ]
then
	[ $? == 0 ] &&
	${CMAKE_DIR}cmake --install ${BUILD_DIR} --prefix=${OUTPUT_DIR} -v
fi

# cp -f ${OUTPUT_DIR}lib/libespcn.so src/main/jniLibs/${ABI}/

set +xv
